package com.ethink.framework.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ethink.framework.common.web.context.RequestContext;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.framework.mybatis.properties.MybatisPlusProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义填充公共字段
 *
 * @author yunduo
 * @date 2020-06-01
 */
@Component
@Slf4j
public class DateMetaObjectHandler implements MetaObjectHandler {

    public final static String MERCHANT_ID = "merchantId";
    public final static String IS_DELETED = "isDeleted";
    public final static String CREATE_TIME = "createTime";
    public final static String UPDATE_TIME = "updateTime";
    public final static String CREATE_USER = "createUser";
    public final static String UPDATE_USER = "updateUser";


    private MybatisPlusProperties properties;

    public DateMetaObjectHandler(MybatisPlusProperties properties) {
        this.properties = properties;
    }

    /**
     * 是否开启了插入填充
     */
    @Override
    public boolean openInsertFill() {
        return properties.getEnableInsertFill();
    }

    /**
     * 是否开启了更新填充
     */
    @Override
    public boolean openUpdateFill() {
        return properties.getEnableUpdateFill();
    }

    /**
     * 插入填充，字段为空自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        RequestContext requestContext = SPRequestContextHolder.getRequestContext();
        Long merchantIdValue = SPRequestContextHolder.getMerchantId();

        //如果是消费者，没有商户id的情况下，使用默认1
        if (merchantIdValue == null) {
            merchantIdValue = 1L;
        }
        Object merchantId = getFieldValByName(MERCHANT_ID, metaObject);
        if (merchantId == null) {
            this.strictInsertFill(metaObject, MERCHANT_ID, Long.class, merchantIdValue);
        }

        Object createUser = getFieldValByName(CREATE_USER, metaObject);
        if (createUser == null) {
            createUser = SPRequestContextHolder.getCurrentUser();
            this.strictInsertFill(metaObject, CREATE_USER, String.class, createUser.toString());
        }
        Object isDelete = getFieldValByName(IS_DELETED, metaObject);
        Boolean isDeleteValue = isDelete == null ? false : (Boolean) isDelete;
        this.strictInsertFill(metaObject, IS_DELETED, Boolean.class, isDeleteValue);
        this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, LocalDateTime.now());
        log.info("insertFill data:merchantId:{},createUser:{},isDeleted:{}", merchantIdValue, createUser, isDeleteValue);
    }

    /**
     * 更新填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        RequestContext requestContext = SPRequestContextHolder.getRequestContext();
        // 填充操作人
        Object updateUser = getFieldValByName(UPDATE_USER, metaObject);
        if (updateUser == null) {
            updateUser = SPRequestContextHolder.getCurrentUser();
            this.strictUpdateFill(metaObject, UPDATE_USER, String.class, updateUser.toString());
        }
        // 填充当前时间
        this.strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now());
        log.info("updateFill data:updateUser:{}", updateUser);
    }


}